package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.service.PullTokensService;
import com.robotserver.orders.service.TbOrderService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TbPayOrderSldSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(TbPayOrderSldSyncXxlJob.class);

    @Autowired
    private TbOrderService tbOrderService;
    /**
     * 生产淘宝会员账号付款订单的tokens
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    //@XxlJob("pushTbPayOrdersSldHander_producer")
    public void pushTbPayOrdersSldHander_producer() {
        logger.info("--------TbPayOrderSldSyncXxlJob pushTbPayOrdersSldHander_producer------start------");
        //默认持续时间为1分钟
        Long duration = 60L;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Long.parseLong(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 60L;
        }
        Integer tmptime = DateUtil.getNowTime();
        Long end_time = Long.valueOf(tmptime);                //截至时间为当前时间
        Long begin_time = tmptime - duration;                 //起始时间为前1分钟

        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
        tbOrderSyncParam.setBegin_time(begin_time);
        tbOrderSyncParam.setEnd_time(end_time);
        //tbOrderSyncParam.setType(0); //付款订单
        tbOrderSyncParam.setOrderScene(3l); //会员
        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbPayOrderSldQueue);
        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
        tbOrderService.syncOrder_producer(tbOrderSyncParam);
        logger.info("-------------往redis中生产淘宝联盟的会员账号付款订单------");
    }

    /**
     * 消费淘宝联盟会员账号tokens拉取各种订单信息
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    //@XxlJob("pushTbPayOrdersSldHander_consumer")
    public void pushTbPayOrdersSldHander_consumer() {
        logger.info("--------TbPayOrderSldSyncXxlJob pushTbPayOrdersSldHander_consumer------start------");
        //调用消费者线程，遍历redis队列，消费alimmTokens
        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbPayOrderSldQueue);
        tbOrderService.syncOrder_consumer(tbOrderSyncParam);
        logger.info("---------------------------消费淘宝联盟的会员账号付款订单-----------------");
    }

}
